package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

/**
 * 结算交易控制
 *
 * @author sunmi on 2017/2/10.
 */
public class SettlementTransactionOperation {

    private static SettlementTransactionOperation instance;
    private SettlementTransaction instanceSettings;

    private SettlementTransactionOperation() {
        instanceSettings = getSettlementTransaction();
        if (instanceSettings == null) {
            instanceSettings = new SettlementTransaction();
        }
        saveSettlementTransaction(instanceSettings);
    }

    public static SettlementTransactionOperation getInstance() {
        if (instance == null) {
            instance = new SettlementTransactionOperation();
        }
        return instance;
    }

    public SettlementTransaction getSettlementTransaction() {
        if(instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(SettlementTransaction.SETTLEMENT_TRANSACTION_SETTING_KEY);
            if (obj instanceof SettlementTransaction) {
                instanceSettings = (SettlementTransaction) obj;
            } else {
                instanceSettings = new SettlementTransaction();
                saveSettlementTransaction(instanceSettings);
            }
        }
        return instanceSettings;
    }

    /**
     * 保存是否自动签退
     *
     * @param isAutomaticSignOut 1是，0否,2未存储
     */
    public void saveAutomaticSignOut(boolean isAutomaticSignOut) {
        SettlementTransaction settlement = getSettlementTransaction();
        settlement.setAutomaticSignOut(isAutomaticSignOut);
        saveSettlementTransaction(settlement);
    }

    /**
     * 得到缓存是否自动签退
     *
     * @return 1是，0否,2未存储
     */
    public boolean getIsAutomaticSignOut() {
        SettlementTransaction settlement = getSettlementTransaction();
        return settlement.isAutomaticSignOut();
    }

    public void saveSettlementTransaction(SettlementTransaction settlementTransaction) {
        this.instanceSettings = settlementTransaction;
        SharedPreferencesUtil.saveObj(settlementTransaction, SettlementTransaction.SETTLEMENT_TRANSACTION_SETTING_KEY);
    }

    /**
     * 清除结算交易控制的存储
     */
    public void clearSettlementTransaction() {
        SharedPreferencesUtil.clearByKey(SettlementTransaction.SETTLEMENT_TRANSACTION_SETTING_KEY);
    }

}
